/**
 * 
 */
package com.junge.jedis;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import com.junge.javafx.HelloWorldController;
import com.junge.utils.JacksonUtil;
import com.junge.utils.NetTools;
import com.junge.utils.StringUtil;

import redis.clients.jedis.Jedis;

/**
 * 类描述 :
 * @author xiaojun.yin
 * @version 1.0
 * 2019年1月17日 下午2:21:50
 */
public class JedisTool {
	static String message = "";
	static AtomicBoolean  flag = new AtomicBoolean(true);
	static AtomicInteger enir = new AtomicInteger(0);
	
	/**
	 * 方法功能描述 :
	 * @author xiaojun.yin
	 * 2019年1月17日 下午2:23:20
	 * @param helloWorldController 
	 */
	public static void synchronizationNeiWangToKaFa(HelloWorldController helloWorldController) {
		message = "同步中... 已耗时：0ms";
		flag.set(true);
		new Thread(new Runnable() {
			public void run() {
				while (flag.get()) {
					try {
						Thread.sleep(180);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					helloWorldController.showLogMessage(message);
				}
			}
		}).start();
		new Thread(new Runnable() {
			public void run() {
				try {
					long start = System.currentTimeMillis();
					JedisFactory neiwang = new JedisFactory("200.200.200.52", 6379);
					JedisFactory kaifa = new JedisFactory("10.8.9.13", 6379, "clife-dev");
					Jedis neiWangJedis = neiwang.getJedis();
					Jedis kaifaJedis = kaifa.getJedis();
					String[] keymeaths = { "AccountToken:Access:31038:*", "AccountToken:Refresh:31038:*",
							"AccountToken:Expires:31038:*", "AccountToken:Account:31038:*" };
					for (String keymeath : keymeaths) {
						Set<String> keys = neiWangJedis.keys(keymeath);
						for (String key : keys) {
							String value = neiWangJedis.get(key);
							String str = "同步==》key[" + key + "]\nvalue=" + value;
							System.out.println(str);
							String result = kaifaJedis.set(key, value);
							System.out.println("同步情况：" + result);
							message = "同步中... 已耗时：" + (System.currentTimeMillis() - start) + "ms";
						}
					}
					neiwang.closeJedisPool();
					kaifa.closeJedisPool();
					message = "同步完成==》耗时：" + (System.currentTimeMillis() - start) + "ms";
					// JOptionPane.showMessageDialog(null, "同步成功！");
				} catch (Exception e) {
					message = "同步失败==》" + e.getMessage();
					// JOptionPane.showMessageDialog(null, "同步失败！"+StringUtil.showStringLength(e.getMessage(), 20) );
				}
				flag.set(false);
			}
		}).start();
	}
	
	/**
	 * 方法功能描述 :
	 * @author xiaojun.yin
	 * 2019年1月27日 下午3:42:03
	 * @param account
	 * @param pwd
	 * @return
	 * @throws Exception 
	 */
	public static UserInfo getUserTokenInfo(String account, String pwd) throws Exception {
		if(StringUtil.isEmpty(account) || StringUtil.isEmpty(pwd)){
			return new UserInfo("用户名或密码不能为空");
		}
		UserInfo userInfo = UserInfoTool.getUserInfoByAccount(account);
		if(userInfo == null){
			Integer accountId = NetTools.getAccountIdByAccount(account,pwd);
			if(accountId == null){
				return new UserInfo("未找到用户信息！");
			}
			userInfo = new UserInfo("不认识", accountId, account, pwd);
			UserInfoTool.insertUserInfo(userInfo);
		}
		JedisFactory neiwang = getJedisFactor();
		String token = shaiXuan(neiwang,"AccountToken:Account:31038:*",userInfo.getAcountId());
		userInfo.setAccessToken(token);
		return userInfo;
	}
	

	private static String shaiXuan(JedisFactory jf,String keymeath,Integer accountId) {
		Jedis jedis = jf.getJedis();
		Set<String> keys= jedis.keys(keymeath);
		for (String key : keys) {
			Map map = JacksonUtil.readObjectValue(jedis.get(key), Map.class);
			if(accountId.toString().equals(String.valueOf(map.get("accountId")))){
				return  String.valueOf(map.get("accessToken"));
			}
		}
		return "未找到";
	}
	/**
	 * 方法功能描述 :获取jedis
	 * @author xiaojun.yin
	 * 2019年2月25日 下午4:49:25
	 * @return
	 */
	private static JedisFactory getJedisFactor() {
		if(0 == enir.get()){
			return new JedisFactory("200.200.200.52", 6379);
		}else if(1 == enir.get()){
			return new JedisFactory("119.29.102.187",6379,"cTInPZQan7mnxQ",1);
		}else if(2 == enir.get()){
			return new JedisFactory("10.8.9.13", 6379, "clife-dev");
		}
		return new JedisFactory("200.200.200.52", 6379);
	}

	/**
	 * 方法功能描述 :
	 * @author xiaojun.yin
	 * 2019年2月25日 下午5:21:44
	 * @param i
	 */
	public static void changeEnir(int index) {
		enir.set(index);
		
	}
}
